using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;
using System.IO;

public partial class Admin_SanPhamThem : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            LoadDropDownList();
        }
        // mac dinh cho ngay thang 
        txt_ngaybatdau.Text = DateTime.Now.ToString("dd/MM/yyyy");
        
    }
    private void LoadDropDownList()
    {
        tbDanhMucCollection dmCollection = tbDanhMucBUS.GetAll();
        ddl_danhmuc.DataTextField = tbDanhMucColumns.TenDanhMuc.ToString();
        ddl_danhmuc.DataValueField = tbDanhMucColumns.MaDM.ToString();
        ddl_danhmuc.DataSource = dmCollection;
        ddl_danhmuc.DataBind();
        ddl_danhmuc.Items.Insert(0, new ListItem("All", "0"));

        tbDoanhNghiepCollection dnCollection = tbDoanhNghiepBUS.GetAll();
        ddl_doanhnghiep.DataTextField = tbDoanhNghiepColumns.TenDN.ToString();
        ddl_doanhnghiep.DataValueField = tbDoanhNghiepColumns.MaDN.ToString();
        ddl_doanhnghiep.DataSource = dnCollection;
        ddl_doanhnghiep.DataBind();
        ddl_doanhnghiep.Items.Insert(0, new ListItem("All", "0"));

        tbNhaSXCollection nsxCollection = tbNhaSXBUS.GetAll();
        ddl_nhasx.DataTextField = tbNhaSXColumns.TenNSX.ToString();
        ddl_nhasx.DataValueField = tbNhaSXColumns.MaNSX.ToString();
        ddl_nhasx.DataSource = nsxCollection;
        ddl_nhasx.DataBind();
        ddl_nhasx.Items.Insert(0, new ListItem("All", "0"));

        tbTinhTrangCollection ttCollection = tbTinhTrangBUS.GetAll();
        ddl_tinhtrang.DataTextField = tbTinhTrangColumns.TenTinhTrang.ToString();
        ddl_tinhtrang.DataValueField = tbTinhTrangColumns.MaTT.ToString();
        ddl_tinhtrang.DataSource = ttCollection;
        ddl_tinhtrang.DataBind();
        ddl_tinhtrang.Items.Insert(0, new ListItem("All", "0"));

        tbDiaDiemCollection ddCollection = tbDiaDiemBUS.GetAll();
        ddl_bando.DataTextField = tbDiaDiemColumns.TenDD.ToString();
        ddl_bando.DataValueField = tbDiaDiemColumns.MaDD.ToString();
        ddl_bando.DataSource = ddCollection;
        ddl_bando.DataBind();
        ddl_bando.Items.Insert(0, new ListItem("All", "0"));

        tbPhuongThucThanhToanCollection ptttCollection = tbPhuongThucThanhToanBUS.GetAll();
        ddl_phuongthucthanhtoan.DataTextField = tbPhuongThucThanhToanColumns.TenPTTT.ToString();
        ddl_phuongthucthanhtoan.DataValueField = tbPhuongThucThanhToanColumns.MaPTTT.ToString();
        ddl_phuongthucthanhtoan.DataSource = ptttCollection;
        ddl_phuongthucthanhtoan.DataBind();
        ddl_phuongthucthanhtoan.Items.Insert(0, new ListItem("All", "0"));
       
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            tbSanPham sanpham = new tbSanPham();
            sanpham.TenSP = txt_tensp.Text;
            sanpham.DanhMuc =int.Parse (ddl_danhmuc.SelectedValue );
            sanpham.DoanhNghiep =int.Parse ( ddl_doanhnghiep.SelectedValue );
            sanpham.NhaSX =int.Parse ( ddl_nhasx.SelectedValue );
            sanpham.GiaGoc =float.Parse ( txt_giagoc.Text);
            sanpham.GiaBan =float.Parse ( txt_giaban.Text);
            sanpham.DiaDiem = int.Parse(ddl_bando.SelectedValue);
            sanpham.NgayBatDau =DateTime .Parse ( txt_ngaybatdau.Text);
            sanpham.NgayHetHan =DateTime .Parse ( txt_ngayhethan.Text);
            sanpham.SoLuongBan =int.Parse ( txt_soluongban.Text);
            sanpham.SoLuongToiDa =int.Parse ( txt_soluongtoida.Text);
            sanpham.MoTa = ftb_mota.Text;
            sanpham.QuyDinhSuDung = txt_quydinhsudung.Text ;
            sanpham.ChietKhau = float .Parse (txt_chietkhau.Text );
            sanpham.TinhTrang =int.Parse ( ddl_tinhtrang.SelectedValue );
            sanpham.PhuongThucThanhToan = int.Parse(ddl_phuongthucthanhtoan.SelectedValue);
            sanpham.LoiGioiThieu = txt_loigioithieu.Text;

            //int hinh = tbSanPhamBUS.Insert(sanpham);

            //HttpFileCollection hfc = Request.Files;
            //for (int i = 0; i < hfc.Count; i++)
            //{
            //    HttpPostedFile hpf = hfc[i];
            //    if (hpf.ContentLength > 0)
            //    {
            //        string filename = DateTime.Now.Ticks.ToString() + "_" + hpf.FileName;
            //        fu_up_hinh.SaveAs(Server.MapPath("~/Photos/" + filename));

            //        System.Drawing.Image img = System.Drawing.Image.FromFile(
            //            Server.MapPath("~/Photos/" + filename));
            //        ImageUtility.MakeThumbnailPhoto(img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);
            //        tbHinhAnh hinhAnh = new tbHinhAnh();
            //        hinhAnh.MaSP = hinh;
            //        hinhAnh.HinhAnh = filename;
            //        tbHinhAnhBUS.Insert(hinhAnh);
            int id = tbSanPhamBUS.Insert(sanpham);

            HttpFileCollection hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    //hpf.SaveAs(Server.MapPath("Photos") + "_" + Path.GetFileName(hpf.FileName));
                    string filename = DateTime.Now.Ticks.ToString() + "_" + Path.GetFileName(hpf.FileName);
                    hpf.SaveAs(Server.MapPath("~/Admin/Photos/" + filename));

                    System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/Admin/Photos/" + filename));
                    ImageUtility.MakeThumbnailPhoto(img, 100, 100, Server.MapPath("~/Admin/Photos/Thumb/"), filename);

                    tbHinhAnh ha = new tbHinhAnh();
                    ha.MaSP = id;
                    ha.HinhAnh = filename;
                    tbHinhAnhBUS.Insert(ha);

                }
            }


            

        }
        catch (Exception ex)
        {

        }
      Response.Redirect("~/Admin/SanPhamDanhSach.aspx");

    }
}